package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Field;
import com.portal.logic.FieldDAO;
import com.portal.logic.SerializationUtil;
import com.portal.logic.Type;
import com.portal.logic.TypeDAO;

public class AddTypeScr extends JFrame {

	private JPanel contentPane;
	private JTextField textType;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddTypeScr frame = new AddTypeScr();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddTypeScr() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblSelectField = new JLabel("Select Field of new Type");
		lblSelectField.setBounds(31, 45, 152, 33);
		contentPane.add(lblSelectField);

		Connection connection = ConnectionManager.getConnection();
		final FieldDAO fieldDAO = new FieldDAO(connection) ;
		List<Field> fields = fieldDAO.selectAll();

		final JComboBox cmbFields = new JComboBox();
		Iterator<Field> iterator = fields.iterator();
		while (iterator.hasNext()) {
			Field field = (Field) iterator.next();
			cmbFields.addItem(field.getName());
		}


		cmbFields.setBounds(207, 50, 85, 22);
		contentPane.add(cmbFields);

		textType = new JTextField();
		textType.setBounds(207, 112, 85, 22);
		contentPane.add(textType);
		textType.setColumns(10);

		JLabel lblEnterNameOf = new JLabel("Enter Name of new Type");
		lblEnterNameOf.setBounds(31, 115, 274, 16);
		contentPane.add(lblEnterNameOf);

		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(12, 217, 97, 25);
		contentPane.add(btnBack);


		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				String selectedField = (String)cmbFields.getSelectedItem();
				Field field = fieldDAO.selectByName(selectedField);
				String addedType = textType.getText();
				if(!"".equals(addedType)){
					TypeDAO typeDAO = new TypeDAO(ConnectionManager.getConnection());
					com.portal.logic.Type type = new com.portal.logic.Type();
					type.setId(typeDAO.selectMaxId()+1);
					type.setIdField(field.getId());
					type.setName(addedType);
					
					try {
						SerializationUtil.serialize(type, "c:\\type.out");
					} catch (IOException ioe) {
						ioe.printStackTrace();
					}
					
					typeDAO.insert();
					JOptionPane.showMessageDialog(null, "New Type was added!");
				}else{
					JOptionPane.showMessageDialog(null, "New value for Type can not be empty.");
				}

			}
		});
		btnSubmit.setBounds(196, 160, 97, 25);
		contentPane.add(btnSubmit);
	}
}
